const app = getApp();
const apis = require("../../apis/request")
let timer = null
const helper = require("../../utils/helper")
Page({
  data: {
    isFullScreen: true,
    dataList: [],
    dataTypeList: [],
    scrollCurrentId: 0,
    swiperCurrentId: 0,
    currentItem: {}, // 当前题目item
    searchValue: '',
    animationVisiable: false,
    bannerList: [
      {
        id: 1,
        img: 'https://img.wlcod.com/red-packet-mall/img/banner1.webp'
      },
      {
        id: 2,
        img: 'https://img.wlcod.com/red-packet-mall/img/banner2.webp'
      },
    ]
  },
  async onLoad(options) {
    const { data: dataTypeList } = await apis.getRedEnvelopeCoversCategories()
    const { data } = await apis.getRedEnvelopeCovers({ hot: 1, pageSize: 100 })
    const hotType = {
      createdAt: "2023-12-14T09:52:12+08:00",
      id: 0,
      name: "爆款推荐",
      sort: 1,
      updatedAt: "2023-12-18T11:27:27+08:00",
    }
    this.setData({
      dataList: data.filter(item => item.hot),
      dataTypeList: [hotType, ...dataTypeList],
    })
  },
  onShow() {
    this.refreshScreenRatio();
  },
  onResize(res) {
    this.setData({ isFullScreen: res.screenRatio === 1 })
  },
  onReady() {
    this.refreshScreenRatio();
  },
  refreshScreenRatio() {
    this.setData({ isFullScreen: app.utils.isFullScreen() })
  },
  onShareAppMessage() {
    return app.helper.generateShareConfig();
  },
  async getList(categoryId = 1, keyword = '') {
    const { data } = await apis.getRedEnvelopeCovers({ categoryId, keyword, pageSize: 100 })
    return data
  },
  onItemClick(e) {
    const id = e.currentTarget.dataset.item.id
    tt.navigateTo({ url: `/pages/goodsDetail/goodsDetail?id=${id}` });
  },
  async scrollItemClick(e) {
    let scrollCurrentId = e.currentTarget.dataset.id
    this.setData({ scrollCurrentId })
    const dataList = await this.getList(scrollCurrentId)
    this.setData({ dataList })
  },
  async clearInput() {
    const dataList = await this.getList()
    this.setData({ dataList, searchValue: '' })
  },
  handleInput(e) {
    const keyword = e.detail.value

    if (timer) clearTimeout(timer)
    timer = setTimeout(async () => {
      const dataList = await this.getList('', keyword)
      this.setData({ dataList, searchValue: keyword })
    }, 300);
  },
});
